<?php
class Forum_Controller_Front
{
    /**
     * Singleton instance
     * 
     * @var Forum_Controller_Front
     */
    private static $_instance = null;
    
    /**
     * Instance of Forum_Controller_Dispatcher
     * 
     * @var Forum_Controller_Dispatcher
     */
    private $_dispatcher = null;
    
    /**
     * Instance of Forum_Controller_Request
     * 
     * @var Forum_Controller_Request
     */
    private $_request = null;
    
    /**
     * Instance of Forum_Controller_Response
     * 
     * @var Forum_Controller_Response
     */
    private $_response = null;
    
    /**
     * 
     * @return Forum_Controller_Front
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * 
     * @return Forum_Controller_Dispatcher
     */
    public function getDispatcher()
    {
        if (!$this->_dispatcher instanceof Forum_Controller_Dispatcher) {
            $this->_dispatcher = new Forum_Controller_Dispatcher();
        }
        return $this->_dispatcher;
    }
    
    /**
     * @return Forum_Controller_Request
     */
    public function getRequest()
    {
        return $this->_request;
    }
    
    /**
     * @return Forum_Controller_Response
     */
    public function getResponse()
    {
        return $this->_response;
    }
    
    /**
     * Dispatch an HTTP request to a controller/action.
     *
     * @return void
     */
    public function dispatch()
    {
        $dispather = $this->getDispatcher();
        
        if (null === $this->_request) {
            $this->_request = new Forum_Controller_Request();
        }
        
        if (null === $this->_response) {
            $this->_response = new Forum_Controller_Response();
        }
        
        $dispather->dispatch($this->_request, $this->_response);
        
        if (Forum_Layout::getMvcInstance()->isEnabled()) {
            Forum_Layout::getMvcInstance()->dispatchLoopShutdown();
        }
            
        $this->_response->sendResponse();
    }
}